<?php

namespace app\index\controller;
use app\index\Controller;
use think\Db;
use think\Validate;
/**
 * @title 司机导游（模块05）
 * @description 接口说明
 * @group 用户
 * @header name:model_id   default:05 desc:模块id
 * @groupremark 已使用错误码： 1050001 1050002 1050003 1050004 1050005 1050006 1050007 1050008 1050009 1050010 1050011 1050012 1050013 1050014 1050015
 */
class Driver extends Controller{
    /**
     * @title 获取司机导游信息
     * @description 接口说明
     * @author 梁敏
     * @url /index/driver/index
     * @param name:token type:int require:1  other: desc:token
     * @param name:user_id type:int require:1  other: desc:user_id
     * @method ~
     * @return data:司机导游信息信息@!
     * @data account:账户 last_login_time:最后登录时间 last_login_ip:最后登录ip mobile:手机 email:电子邮箱 status:状态 remark:备注
     */
    public function index() {

        //获取数据
        $data=input();

        $vmsg = [
            'user_id.require'   => '未指定用户',
            'token.require'  => '缺少token',
        ];
        $validate = new Validate([
            'token'  => 'require',
            'user_id'=>"require",
        ],$vmsg);

        if (!$validate->check($data)) {
            $msg['error_id']="1050001";
            $msg['message']=$validate->getError();
            return $this->error($msg);
        }
        $user=user($data['user_id']);
        $driver=Db::name('driver')->where("user_id",$data['user_id'])->find();

        if($user&&$driver){
            if($driver){
                $msg['data']=array_merge($user,$driver);
            }else{
                $msg['data']=$user;
            }

            return $this->success($msg);
        }else{
            $msg['error_id']="1050002";
            $msg['message']="不存在或未填写信息";
            return $this->error($msg);
        }

    }

    /**
     * @title 更新司机导游头像
     * @description 接口说明
     * @author 梁敏
     * @param name:token type:int require:1  other: desc:token
     * @param name:user_id type:int require:1  other: desc:user_id
     * @param name:head_img type:string require:1  other: desc:头像
     *
     * @url /index/driver/update_head
     * @method ~
     *  @return error_id: 1050015 校验错误
     */
    public function update_head() {

        //获取数据
        $data=input();

        $vmsg = [
            'user_id.require'   => '未指定用户',
            'token.require'  => '缺少token',
            'head_img'=>'缺少头像',
        ];
        $validate = new Validate([
            'user_id'   => 'require',
            'token'  => 'require',
            'head_img'=>'require',
        ],$vmsg);

        if (!$validate->check($data)) {
            $msg['error_id']="1050015";
            $msg['message']=$validate->getError();
            return $this->error($msg);
        }
        //$msg=$this->update_driver($data['user_id'],$data,$data);

        //根据传递过来的数据更新admin_user表
        if($data){
            unset($data['id']);
            $re=Db::name("admin_user")->where("id",$data['user_id'])->update($data);
        }
        $_driver=Db::name("driver")->where("user_id",$data['user_id'])->find();
        unset($_driver['id']);
        if($_driver){
            $re=Db::name("driver")->where("user_id",$data['user_id'])->update($data);
        }else{
            $re=Db::name("driver")->insert($data);
        }
        if($re!==false){
            $msg['message']="提交成功";
            return $this->success($msg);
        }

        $msg['error_id']="1050015";
        $msg['message']="更新失败";
        return $this->error($msg);
        //return $msg;

    }


    /**
     * @title 更新司机导游基本信息
     * @description 接口说明
     * @author 梁敏
     * @param name:token type:int require:1  other: desc:token
     * @param name:user_id type:int require:1  other: desc:user_id
     * @param name:realname type:int require:1  other: desc:姓名
     * @param name:sex type:int require:1  other: desc:性别boy男girl女
     * @param name:birthday type:int require:1  other: desc:birthday
     * @param name:nationality type:string require:1  other: desc:民族
     * @param name:weixin_no type:int require:1  other: desc:微信号
     * @param name:domicile type:int require:1  other: desc:居住地
     * @param name:head_img type:string require:1  other: desc:头像
     *
     * @url /index/driver/update_base
     * @method ~
     *  @return error_id: 1050003 校验错误
     */
    public function update_base() {

        //获取数据
        $data=input();
        $time = date('Y-m-d',time());
        logs("{$time}updatebase", $data);
        $vmsg = [
            'user_id.require'   => '未指定用户',
            'token.require'  => '缺少token',
            'realname.require'=>'未填写姓名',
            'sex.require'  => '缺少性别',
            'birthday.require'  => '缺少生日',
            'weixin_no.require'  => '缺少微信号',
            'nationality.require'  => '缺少民族',
//            'hometown'=>'缺少家乡',
            'domicile.require'  => '缺少居住地',
            'head_img.require'  => '缺少头像',
        ];
        $validate = new Validate([
            'user_id'   => 'require',
            'token'  => 'require',
            'realname'  => 'require',
            'sex'  => 'require',
            'birthday'  => 'require',
            'nationality'  => 'require',
            'weixin_no'  => 'require',
            'domicile'  => 'require',
            'head_img'  => 'require',
//            'hometown'  => 'require',
        ],$vmsg);

        if (!$validate->check($data)) {
            $msg['error_id']="1050003";
            $msg['message']=$validate->getError();
            return $this->error($msg);
        }
        //$msg=$this->update_driver($data['user_id'],$data,$data);

        //根据传递过来的数据更新admin_user表
        if($data['mobile']){

            $re=Db::name("admin_user")->where("account",$data['mobile'])->find();
            if($re&&$data['user_id']!=$re['id']){
                $msg['error_id']="1050003";
                $msg['message']="手机号已经绑定其他账号";
                return $this->error($msg);
            }
            $data['account']=$data['mobile'];
        }

        unset($data['id']);
        if($data){
            $re=Db::name("admin_user")->where("id",$data['user_id'])->update($data);
        }
        $_driver=Db::name("driver")->where("user_id",$data['user_id'])->find();
        unset($_driver['id']);
        if($_driver){
            $re=Db::name("driver")->where("user_id",$data['user_id'])->update($data);
        }else{
            $msg['error_id']="1050005";
            $msg['message']="更新失败";
            return $this->error($msg);
//            $re=Db::name("driver")->insert($data);
        }
        if($re!==false){
            $msg['message']="提交成功";
            return $this->success($msg);
        }

        $msg['error_id']="1050005";
        $msg['message']="更新失败";
        return $this->error($msg);
        //return $msg;

    }

    /**
     * @title 更新司机导游司机信息
     * @description 照片传的全是路径
     * @author 李锐志
     * @param name:token type:string require:1  other: desc:token
     * @param name:user_id type:int require:1  other: desc:user_id
     * @param name:id_card type:string require:1  other: desc:身份证
     * @param name:id_card_front_img type:string require:1  other: desc:身份证正面
     * @param name:id_card_back_img type:string require:1  other: desc:身份证反面
     * @param name:take_id_card_img type:string require:1  other: desc:手持身份证照片
     * @param name:driver_license_number type:string require:1  other: desc:驾照号码
     * @param name:driver_license_img type:string require:1  other: desc:驾照照片
     * @param name:driver_license_date type:string require:1  other: desc:驾照有效期
     * @url /index/driver/update_driver
     * @method ~
     * @return error_id: 1050004 用户不存在
     * @return error_id: 1050006 校验错误
     * @return error_id: 1050007 更新失败
     */
    public function update_driver() {

            //获取数据
            $data=input();

            $_user=user($data["user_id"]);
            if(!$_user){
                $msg['error_id']="1050004";
                $msg['message']="用户不存在";
                return $this->error($msg);
            }

            $vmsg = [
                'token.require'  => '缺少token',
                'user_id.require'  => '未指定用户',
                'id_card.require'  => '缺少身份证号',
                'id_card_front_img.require'  => '缺少身份证正面',
                'id_card_back_img.require'  => '缺少身份证反面',
                'take_id_card_img.require'  => '缺少手持身份证照片',
                'driver_license_number.require'  => '缺少驾照号码',
                'driver_license_img.require'  => '缺少驾照照片',
                'driver_license_date.require'  => '缺少驾照有效期',
            ];
            $validate = new Validate([
                'token'  => 'require',
                'user_id'  => 'require',
                'id_card'  => 'require',
                'id_card_front_img'  => 'require',
                'id_card_back_img'  => 'require',
                'take_id_card_img'  => 'require',
                'driver_license_number'  => 'require',
                'driver_license_img'  => 'require',
                'driver_license_date'  => 'require',
            ],$vmsg);

            if (!$validate->check($data)) {
                $msg['error_id']="1050006";
                $msg['message']=$validate->getError();
                return $this->error($msg);
            }
            $map_have_dln['driver_license_number']=$data['driver_license_number'];
            $map_have_dln['user_id']=array('neq',$data['user_id']);
            $re=Db::name("driver")->where($map_have_dln)->find();
            if($re){
                $msg['error_id']="1050006";
                $msg['message']="驾照已存在";
                return $this->error($msg);
            }
            $_driver=Db::name("driver")->where("user_id",$data['user_id'])->find();
            unset($_driver['id']);
            if($_driver){
                $re=Db::name("driver")->where("user_id",$data['user_id'])->update($data);
            }else{
              $msg['error_id']="1050007";
              $msg['message']="更新失败";
              return $this->error($msg);
            }
            if($re!==false){
                $msg['message']="提交成功";
                return $this->success($msg);
            }
    }

    /**
     * @title 更新司机导游汽车信息
     * @description 照片传的全是路径
     * @author 李锐志
     * @param name:token type:string require:1  other: desc:token
     * @param name:user_id type:int require:1  other: desc:user_id
     * @param name:car_brand type:string require:1  other: desc:车品牌
     * @param name:car_type type:string require:1  other: desc:车型
     * @param name:car_model type:string require:1  other: desc:汽车型号
     * @param name:seats type:string require:1  other: desc:座位数
     * @param name:car_product_date type:string require:1  other: desc:出厂日期
     * @param name:plate_number type:string require:1  other: desc:车牌号
     * @param name:driving_permit_date type:string require:1  other: desc:行驶证有效日期
     * @param name:driving_permit_img type:string require:1  other: desc:行驶证照片
     * @param name:insurance type:string require:1  other: desc:商业险照片
     * @param name:car_front type:string require:1  other: desc:车辆正面照片
     * @param name:car_back type:string require:1  other: desc:车辆背面照片
     * @param name:in_car type:string require:1  other: desc:内乘坐区照片
     * @param name:trunk_photo type:string require:1  other: desc:后备箱照片
     * @url /index/driver/update_car
     * @method ~
     * @return error_id: 1050008 用户不存在
     * @return error_id: 1050009 校验错误
     * @return error_id: 1050010 更新失败
     */

     public function update_car(){

        //获取数据
        $data = input();
logs('20190302data',$data);
        $_user = user($data["user_id"]);
        //判断用户是否存在
        if(!$_user){
            $msg['error_id']="1050008";
            $msg['message']="用户不存在";
            return $this->error($msg);
        }

        //验证规则定义
        $vmsg = [
          'token.require' => '缺少token',
          'user_id.require' => '未指定用户',
          'car_brand.require' => '缺少车品牌',
          'car_type.require' => '缺少车型',
          'car_model.require' => '缺少汽车型号',
          'seats.require' => '缺少座位数',
          'car_product_date.require' => '缺少出厂日期',
          'plate_number.require' => '缺少车牌号',
          'driving_permit_date.require' => '缺少行驶证有效日期',
          'driving_permit_img.require' => '缺少行驶证照片',
          'insurance.require' => '缺少商业险险照片',
          'car_front.require' => '缺少车辆正面照片',
          'car_back.require' => '缺少车辆背面照片',
          'in_car.require' => '缺少内乘坐去照片',
          'trunk_photo.require' => '缺少后备箱照片',
        ];
        $validate = new Validate([
          'token' => 'require',
          'user_id' => 'require',
          'car_brand' => 'require',
          'car_type' => 'require',
          'car_model' => 'require',
          'seats' => 'require',
          'car_product_date' => 'require',
          'plate_number' => 'require',
          'driving_permit_date' => 'require',
          'driving_permit_img' => 'require',
          'insurance' => 'require',
          'car_front' => 'require',
          'car_back' => 'require',
          'in_car' => 'require',
          'trunk_photo' => 'require',
        ],$vmsg);

        if(!$validate -> check($data)){
          $msg['error_id']="1050009";
          $msg['message']=$validate->getError();
          return $this->error($msg);
        }

        // 查询车型
         $car_type = Db::name('arctic')->where('id',$data['car_type'])->find();
        if (!$car_type){
            $msg['error_id'] = 1050010;
            $msg['message'] = '不存在该车型信息';
            return $this->error($msg);
        }else{
            $data['car_type'] = $car_type['name']; // 车辆类型
            $data['arctic_id'] = $car_type['id']; // 车类型id
        }
        // 查询车辆品牌
         $car_brand = Db::name('car_brand')->where('id',$data['car_brand'])->find();
         if (!$car_brand){
             $msg['error_id'] = 1050010;
             $msg['message'] = '不存在该车辆品牌信息';
             return $this->error($msg);
         }else{
             $data['car_brand'] = $car_brand['brand_name']; // 汽车品牌
             $data['car_brand_id'] = $car_brand['id']; // 车辆品牌id
         }
         // 查询汽车型号
         $car_model = Db::name('car_model')->where('id',$data['car_model'])->find();
         if (!$car_model){
             $msg['error_id'] = 1050010;
             $msg['message'] = '不存在该汽车型号信息';
             return $this->error($msg);
         }else{
             $data['car_model_id'] = $car_model['id']; // 汽车型号id
             $data['car_model'] = $car_model['car_model']; // 汽车型号
         }

        $_driver=Db::name("driver")->where("user_id",$data['user_id'])->find();
        unset($_driver['id']);
        if($_driver){
            $data['check_status']="applying";
            $re=Db::name("driver")->where("user_id",$data['user_id'])->update($data);
        }else{
          $msg['error_id']="1050010";
          $msg['message']="更新失败";
          return $this->error($msg);
        }
        if($re!==false){
            $msg['message']="提交成功";
            return $this->success($msg);
        }


     }
     /**
      * @title 账号资料修改
      * @description 姓名、性别、手机号在admin_user表中，其余都在driver表中
      * @author 李锐志
      * @param name:token type:string require:1  other: desc:token
      * @param name:user_id type:int require:1  other: desc:user_id
      * @param name:realname type:string require:1  other: desc:姓名
      * @param name:head_img type:string require:1  other: desc:头像
      * @param name:mobile type:string require:1  other: desc:手机号
      * @param name:sex type:string require:1  other: desc:性别
      * @param name:birthday type:string require:1  other: desc:生日
      * @param name:nationality type:string require:1  other: desc:民族
      * @param name:hometown type:string require:1  other: desc:家乡
      * @param name:domicile type:string require:1  other: desc:居住地
      * @param name:weixin_no type:string require:1  other: desc:微信号
      * @param name:id_card type:string require:1  other: desc:身份证
      * @url /index/driver/update_info
      * @method ~
      * @return error_id: 1050011 用户不存在
      * @return error_id: 1050012 校验错误
      * @return error_id: 1050013,1050014 更新失败
      */

      public function update_info(){
        $data = input();

        $_user = user($data["user_id"]);
        //判断用户是否存在
        if(!$_user){
            $msg['error_id']="1050011";
            $msg['message']="用户不存在";
            return $this->error($msg);
        }

        $vmsg = [
          ['token','require','缺少token'],
          ['user_id','require','未指定用户'],
          ['realname','require','缺少姓名'],
          ['head_img','require','缺少头像'],
          ['mobile','require|regex:^1[34578]\d{9}$','缺少手机号|请输入正确的手机号'],
          ['sex','require|in:boy,girl','缺少性别|只能输入boy或girl'],
          ['birthday','require','缺少生日'],
          ['hometown','require','缺少家乡'],
          ['domicile','require','缺少居住地'],
          ['weixin_no','require','缺少微信号'],
          ['id_card','require','缺少身份证号'],
          ['nationality','require','民族不能为空']
        ];

        $validate = new Validate($vmsg);
        $v_re = $validate -> check($data);

        if(!$v_re){
          $msg['error_id']="1050012";
          $msg['message']=$validate->getError();
          return $this->error($msg);
        }

        $data['account']=$data['mobile'];
          $re=Db::name("admin_user")->where("account",$data['mobile'])->find();
          if($re&&$data['user_id']!=$re['id']){
              $msg['error_id']="1050013";
              $msg['message']="手机号已经绑定其他账号";
              return $this->error($msg);
          }
        $user = Db::name('admin_user')->where('id',$data['user_id'])->find();
        if($user){
          $result_user = Db::name('admin_user')->where('id',$data['user_id'])->update($data);
        }else{
          $msg['error_id']="1050013";
          $msg['message']="更新失败";
          return $this->error($msg);
        }
        $_driver=Db::name("driver")->where("user_id",$data['user_id'])->find();
        unset($_driver['id']);
        if($_driver){
          $result_driver=Db::name("driver")->where("user_id",$data['user_id'])->update($data);
        }else{
          $msg['error_id']="1050014";
          $msg['message']="更新失败";
          return $this->error($msg);
        }
        if($result_user!==false&&$result_driver!==false){
          $msg['message']="提交成功";
          return $this->success($msg);
        }
      }


}
